package com.gopay.bis.gdb.ebank.operation;

import java.security.PrivateKey;
import java.security.PublicKey;

import cfca.sadk.cgb.toolkit.BASE64Toolkit;
import cfca.sadk.cgb.toolkit.SM2Toolkit;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.gdb.ebank.gfbank.payment.merchant.SM2SignUtil;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.gdb.ebank.GdbB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelper;

/**
 * 广发银行B2C网关签名验签类
 * 
 * @GdbB2cCertHelper.java
 * @author zhengxue
 * @2014-11-13 上午9:32:24 www.gopay.com.cn Inc.All rights reserved.
 */
public class GdbB2cCertHelper implements ICertHelper {

    @Override
    public String sign(BaseMessage baseMessage) throws GopayException {
        try {
        	GdbB2cMessage messageB2C = (GdbB2cMessage) baseMessage;
            BankLogUtils.printB2CSignPlainLog(messageB2C);

        	//构建SM2工具类对象
			SM2Toolkit tool = new SM2Toolkit();
			byte[] privateByte=SM2SignUtil.read(messageB2C.getPrivateKeyPath());
			
			//对二进制字节数组编码成为BASE64编码字符串
			// 根据SM2私钥BASE64数据构建SM2私钥对象
			PrivateKey privateKey = tool.SM2BuildPrivateKey(BASE64Toolkit.encode(privateByte));
			//签名
			byte[] signArr = tool.SM2Sign(privateKey, messageB2C.getPlainMessage().getBytes("UTF-8"));
			//转码
			String signResult=BASE64Toolkit.encode(signArr);
            if (signResult.length() > 10) {
                // 如果签名成功将返回签名结果，长度远远大于十位
                BankLogUtils.printB2CSignSignatureLog(baseMessage, signResult);
                return signResult;
            } else {
                // 如果签名失败，返回1位的错误代码
                baseMessage.setSpecial("errorCode = " + signResult);
                BankLogUtils.printB2CSignExceptionLog(baseMessage, null);
                throw new GopayException(ProcCodeConstants.PROC_CODE_100E6029);
            }
        } catch (Exception e) {
            BankLogUtils.printB2CSignExceptionLog(baseMessage, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6029);
        }
    }

    @Override
    public Object verify(BaseMessage baseMessage) throws GopayException {
        try {
            GdbB2cMessage messageB2C = (GdbB2cMessage) baseMessage;
            BankLogUtils.printB2CVerifyPlainLog(messageB2C);
            BankLogUtils.printB2CVerifySignatureLog(messageB2C, messageB2C.getSignMessage());

            SM2Toolkit tool = new SM2Toolkit();
			byte[] publicByte=SM2SignUtil.read(messageB2C.getPublicKeyPath());
			//对二进制字节数组编码成为BASE64编码字符串
		    //根据SM2公钥BASE64数据构建SM2公钥对象
			PublicKey publicKey=tool.SM2BuildPublicKey(BASE64Toolkit.encode(publicByte));
			//验签
			boolean verifyResult=tool.SM2Verify(publicKey, baseMessage.getPlainMessage().getBytes("UTF-8"), BASE64Toolkit.decode(messageB2C.getSignMessage()));
            
            return verifyResult;
        } catch (Exception e) {
            BankLogUtils.printB2CVerifyExceptionLog(baseMessage, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6031);
        }
    }
}
